let baseRouter = [{
        id: 1,
        pid: 0,
        component: 'Main',
        path: '/setting',
        hidden: 0,
        title: "系统设置",
        target: '_blank',
        keepAlive: false,
        icon: "setting",
        redirect: '/setting/router',
    },
    {
        id: 2,
        pid: 1,
        path: '/setting/router',
        target: '_self',
        hidden: 0, //在menu  是否隐藏
        title: "设置路由",
        keepAlive: false,
        component: '/setting/router',
        redirect: '',

    },
    {
        id: 3,
        pid: 1,
        path: '/setting/setting',
        target: '_self',
        hidden: 0, //在menu  是否隐藏
        title: "系统设置",
        keepAlive: false,
        redirect: '',
        component: '/setting/setting',
    },
    {
        id: 4,
        pid: 3,
        path: '/setting/edit',
        target: '_self',
        hidden: 1, //在menu  是否隐藏
        title: "系统设置",
        keepAlive: false,
        redirect: '',
        component: '/setting/edit',
    },


]

class MenuController extends require('./base') {
    async index() {
        const { ctx } = this;
        this.success(getlist(ctx.query))
    }
    async update() {
        const { ctx } = this;
        var { id } = ctx.query
        var body = ctx.request.body
        id = id || body.id
        baseRouter = baseRouter.map(val => {
            if (val.id == id) {
                val = body
            }
            return val
        })
        console.log(baseRouter)

        this.success({ data: 'success' })
    }
    async created() {
        const { ctx } = this;
        var id = getid()
        var body = ctx.request.body
        var data = {
            id,
            ...body
        }
        baseRouter.push(data)
        this.success({ data: 'success' })

    }
    async delete() {
        const { ctx } = this;
        baseRouter = baseRouter.filter(val => val.id !== id)
        this.success({ data: 'success' })
    }
    async show() {
        const { ctx } = this;
        var { id } = ctx.query
        var data = baseRouter.filter(val => val.id == id)[0]
        this.success(data)
    }


}

module.exports = MenuController;


function getlist(params) {
    var { current = 1, pid } = params;
    var startIndex = current - 1 * 10
    var endIndex = 10;
    var newlist = JSON.parse(JSON.stringify(baseRouter))
    if (pid) {
        newlist = newlist.filter(val => val.pid == pid)
    }
    let reslist = newlist.splice(startIndex, endIndex);

    return { list: reslist, total: baseRouter.length }
}

function getid() {
    baseRouter.sort((a, b) => {
        return a.id - b.id
    })
    return ++baseRouter[baseRouter.length - 1].id

}